﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;

/// <summary>
/// Supports Shopping Cart functionality
/// </summary>
/// 

public struct Subs{
    public int idSubscription;
    public string length;
    public string name;
    public int cost;
}
public struct UserSub
{
    public int idSubscription;
    public Boolean validBit;
    public DateTime DateSubscribed;
}
public class SubsciptionAccess
{
    public SubsciptionAccess()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    //креирај нарачка
    public static int CreateOrder(int idSubscription, string username)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "CreateOrder";
        DbParameter param1 = comm.CreateParameter();
        param1.ParameterName = "@idSubscription";
        param1.Value = idSubscription;
        param1.DbType = DbType.Int32;
        comm.Parameters.Add(param1);

        param1 = comm.CreateParameter();
        param1.ParameterName = "@username";
        param1.Value = username;
        param1.DbType = DbType.String;
        param1.Size = 250;
        comm.Parameters.Add(param1);

        return Convert.ToInt32(GenericDataAccess.ExecuteScalar(comm));
    }

    //комплетирај нарачка
    public static bool CompleteOrder(int idOrder)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "CompleteOrder";

        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@idOrder";
        parameter.Value = idOrder;
        parameter.DbType = DbType.String;
        comm.Parameters.Add(parameter);

        int result = -1;
        try
        {
            result = GenericDataAccess.ExecuteNonQuery(comm);
        }
        catch
        {
        }
        return (result != -1);
    }

    //откажи нарачка
    public static bool CancelOrder(int idOrder)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "CancelOrder";

        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@idOrder";
        parameter.Value = idOrder;
        parameter.DbType = DbType.String;
        comm.Parameters.Add(parameter);

        int result = -1;
        try
        {
            result = GenericDataAccess.ExecuteNonQuery(comm);
        }
        catch
        {
        }
        return (result != -1);
    }

    //зима subscription за даден ID
    public static Subs GetSubscription(int SubId)
    {
        // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "GetSubscription";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@SubscriptionId";
        param.Value = SubId;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // return the result table
        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        Subs subscription = new Subs();
        if (dataTable.Rows.Count > 0)
        {
            DataRow dr = dataTable.Rows[0];
            subscription.idSubscription = SubId;
            subscription.length = dr["length"].ToString();
            subscription.name = dr["name"].ToString();
            subscription.cost = Convert.ToInt32(dr["cost"]);
        }
        return subscription;
    }

    //ги зема сите типови на претплата
    public static DataSet GetAllSubscriptions()
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "GetAllSubscriptions";
        
        // return the result table
        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        DataSet ds = new DataSet();
        ds.Tables.Add(dataTable);
        return ds;
    }

    //зема subscription на даден корисник
    public static UserSub GetUserSubscription(string UserName)
    {
        // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "GetUserSubscription";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@UserName";
        param.Value = UserName;
        param.DbType = DbType.String;
        comm.Parameters.Add(param);
        // return the result table
        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        UserSub subscription = new UserSub();
        if (dataTable == null)
        {
            subscription.idSubscription = 0;
        }
        else
        {
            if (dataTable.Rows.Count > 0)
            {
                DataRow dr = dataTable.Rows[0];
                subscription.idSubscription = Convert.ToInt32(dr["idSubscription"]);
                subscription.validBit = Convert.ToBoolean(dr["validbit"].ToString());
                subscription.DateSubscribed = Convert.ToDateTime(dr["dateSubscribed"]);
            }
        }
        return subscription;
    }
    
    //менува бит за валидност
    public static void SetSubscriptionBitToFalse(string UserName)
    {
        // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "SetSubscriptionBitToFalse";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@userName";
        param.Value = UserName;
        param.DbType = DbType.String;
        comm.Parameters.Add(param);
        GenericDataAccess.ExecuteNonQuery(comm);
    }

}
